package javanetworking;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Admin
 */
public class Send implements Runnable {

    DatagramSocket socket;
    InetAddress address;
    int port;
    //private String msg;
    private byte[] pgk;
    private DatagramPacket packet;
    //send khi da nhan goi tin packet tu client
    public Send(DatagramSocket socket, DatagramPacket repacket, String msg) throws Exception {
        this.socket = socket;
        this.address = repacket.getAddress();
        this.port = repacket.getPort();
        this.pgk = msg.getBytes();
        Thread t = new Thread(this);
        t.start();

    }
    //send den mot host bat ky voi du lieu la chuoi
    public Send(DatagramSocket socket, String host, int port, String msg) throws Exception {
        this.socket = socket;
        this.address = InetAddress.getByName(host);
        this.port = port;
        this.pgk = msg.getBytes();
        Thread t = new Thread(this);
        t.start();

    }
    // send den host bat ky voi du lieu la byte[]
    public Send(DatagramSocket socket, String host, int port, byte[] packet) throws Exception {
        this.socket = socket;
        this.address = InetAddress.getByName(host);
        this.port = port;
        this.pgk = packet;
        this.packet=new DatagramPacket(this.pgk,this.pgk.length);
//        Thread t = new Thread(this);
//        t.start();
    }

    @Override
    public void run() {
        
        this.packet.setAddress(this.address);
        this.packet.setPort(this.port);
        try {
            this.socket.send(packet);
            //DatagramSocket sk=new DatagramSocket();
            //sk.send(packet);
            //System.out.println("Send to: <"+this.address+">:"+this.port);
            //this.socket.close();
        } catch (IOException ex) {
            Logger.getLogger(Send.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   
}
